/**
 * create by 9-3-17
 * 
 */
const _ = require('lodash');
calculateController.$inject = ['$scope','$location','calculateService'];

function calculateController($scope,$location,calculateService){
    
    var cc = this;
    let sh = sessionStorage.getItem("shoppingCart");
    cc.shoppingCart= JSON.parse(sh);
    $scope.addressNullMessage = '';
    calculateService.calTotalMoney(cc.shoppingCart, (err, result) => {
        cc.totalMoney = result;
    });

    
    cc.plusOneItem = function plusOneItem(food){
        let customer = sessionStorage.getItem('customer');
        console.log(customer);
        food.num++;
        food.subTotalMoney = food.num * food.price;
        calculateService.calTotalMoney(cc.shoppingCart, (err, result) => {
            cc.totalMoney = result;
        });
    }

    cc.minusOneItem = function minusOneItem(food){
        if(food.num > 0){
            food.num--;
            food.subTotalMoney = food.num * food.price;
        }
        calculateService.calTotalMoney(cc.shoppingCart, (err, result) => {
            cc.totalMoney = result;
        });
    }

    cc.removeOrderItemFromShoppingCart = function removeOrderItemFromShoppingCart(food){
        let index = 0;
        // let newShoppingCart = [];
        _.forEach(cc.shoppingCart, (item)=>{
            

            console.log(item._id);
            console.log(food._id);
            if(item._id == food._id){
                console.log("drop...........");
                cc.shoppingCart.splice(index, 1);
                // sessionStorage.setItem("shoppingCart", JSON.stringify(temp));
            }else{
                // newShoppingCart.push(item);
            }
            // sessionStorage.setItem("shoppingCart", JSON.stringify(temp));
            sessionStorage.setItem("shoppingCart", JSON.stringify(cc.shoppingCart));
            index++;
        })
    }

    //结算：从session里面拿到所有需要拿的数据
    cc.payFromOrderInfo = function payFromOrderInfo(){
        console.log("结算信息如下"); 
        //开始收集订单的信息
        let orderInfo = {};
        orderInfo.merchant = sessionStorage.getItem("merchantId");
        orderInfo.customer = JSON.parse(sessionStorage.getItem('customer')).id;
        orderInfo.foods = [];
        let shoppingCart = JSON.parse(sessionStorage.getItem('shoppingCart'));
        _.forEach(shoppingCart, (item) => {
            console.log(item.price+" "+ item.name +" "+item.subTotalMoney);
            let food = {};
            food.subTotalMoney = item.subTotalMoney;
            food.num = item.num;
            food.food = item._id;
            orderInfo.foods.push(food);
        });
        let selectedAddr = sessionStorage.getItem('selectedAddr');
        console.log("selectedAddr____"+selectedAddr);

        try{
            if(selectedAddr.length > 0){
                $scope.addressNullMessage = '';
                let receiverInfo = selectedAddr.split("  ");   
                orderInfo.address = receiverInfo[0];
                orderInfo.phone = receiverInfo[1];
                orderInfo.receiver = receiverInfo[2];
                orderInfo.allTotalMoney = cc.totalMoney;
                console.log("______address____"+orderInfo.address);
                console.log("_____receiver_____"+orderInfo.receiver);
                console.log("____allTotalMoney______"+orderInfo.allTotalMoney);
                let sendInfo = JSON.stringify(orderInfo);
                console.log("要发送的信息如下 "+sendInfo);
                calculateService.payFromOrderInfo(JSON.stringify(orderInfo), (err, result) => {
                    let shoppingCart = [];
                    sessionStorage.setItem("shoppingCart", JSON.stringify(shoppingCart));
                    sessionStorage.setItem('selectedAddr', null);
                    $location.path("/");
                });   
            }else{
                $scope.addressNullMessage = "请添加【收货地址】信息";
            }
        }catch(e){
            $scope.addressNullMessage = "请添加【收货地址】信息";
        }

        
       
    }

}

module.exports = calculateController;